BicycleSeeder.run   C
last analyzed

Complexity

Conditions 5

Size

Total Lines 184
Code Lines 141

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 141
dl 0
loc 184
ccs 19
cts 19
cp 1
crap 5
rs 6.5333
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { DataSource } from 'typeorm';
2 7
import { Bicycle } from '../../bicycles/entities/bicycle.entity';
3 7
import { City } from '../../cities/entities/city.entity';
4 7
import { CityName } from '../../cities/types/city.enum';
5
6 7
export default class BicycleSeeder {
7
  async run(connection: DataSource): Promise<void> {
8 9
    if (true) {
9
      //process.env.NODE_ENV !== 'production') {
10 9
      const bicycleRepo = connection.getRepository(Bicycle);
11 9
      const cityRepo = connection.getRepository(City);
12
13
      // keeping debug logs for now
14 9
      let goteborg = await cityRepo.findOne({ where: { name: CityName.Göteborg } });
15 9
      if (!goteborg) {
16
        // console.log('Creating Göteborg...');
17 5
        goteborg = cityRepo.create({ name: CityName.Göteborg });
18 5
        await cityRepo.save(goteborg);
19
        // console.log('Göteborg created with ID:', goteborg.id);
20
      }
21
22 9
      let karlshamn = await cityRepo.findOne({ where: { name: CityName.Karlshamn } });
23 9
      if (!karlshamn) {
24
        // console.log('Creating Karlshamn...');
25 5
        karlshamn = cityRepo.create({ name: CityName.Karlshamn });
26 5
        await cityRepo.save(karlshamn);
27
        // console.log('Karlshamn created with ID:', karlshamn.id);
28
      }
29
30 9
      let jonkoping = await cityRepo.findOne({ where: { name: CityName.Jönköping } });
31 9
      if (!jonkoping) {
32
        // console.log('Creating Jönköping...');
33 5
        jonkoping = cityRepo.create({ name: CityName.Jönköping });
34 5
        await cityRepo.save(jonkoping);
35
        // console.log('Jönköping created with ID:', jonkoping.id);
36
      }
37
38
      // const cities = await cityRepo.find();
39
      // console.log('Available cities:', cities);
40
      // console.log(goteborg)
41
      // console.log(karlshamn)
42
43
      // Add example bicycles
44
      // Göteborg bikes
45
      type BicycleStatus = 'Available' | 'Service' | 'Rented';
46
47 9
      const goteborgBikes = [
48
        // Available bikes
49
        {
50
          batteryLevel: 95,
51
          latitude: 57.7089,
52
          longitude: 11.9726,
53
          status: 'Available' as BicycleStatus,
54
          city: goteborg,
55
        },
56
        {
57
          batteryLevel: 88,
58
          latitude: 57.7095,
59
          longitude: 11.9689,
60
          status: 'Available' as BicycleStatus,
61
          city: goteborg,
62
        },
63
        {
64
          batteryLevel: 92,
65
          latitude: 57.7066,
66
          longitude: 11.9384,
67
          status: 'Available' as BicycleStatus,
68
          city: goteborg,
69
        },
70
        // Service bikes
71
        {
72
          batteryLevel: 15,
73
          latitude: 57.7,
74
          longitude: 11.9753,
75
          status: 'Service' as BicycleStatus,
76
          city: goteborg,
77
        },
78
        {
79
          batteryLevel: 5,
80
          latitude: 57.7048,
81
          longitude: 11.9757,
82
          status: 'Service' as BicycleStatus,
83
          city: goteborg,
84
        },
85
        {
86
          batteryLevel: 25,
87
          latitude: 57.7001,
88
          longitude: 11.9765,
89
          status: 'Service' as BicycleStatus,
90
          city: goteborg,
91
        },
92
      ];
93
94
      // Karlshamn bikes
95 9
      const karlshamnBikes = [
96
        // Available bikes
97
        {
98
          batteryLevel: 90,
99
          latitude: 56.1708,
100
          longitude: 14.8631,
101
          status: 'Available' as BicycleStatus,
102
          city: karlshamn,
103
        },
104
        {
105
          batteryLevel: 85,
106
          latitude: 56.1657,
107
          longitude: 14.8607,
108
          status: 'Available' as BicycleStatus,
109
          city: karlshamn,
110
        },
111
        {
112
          batteryLevel: 93,
113
          latitude: 56.1695,
114
          longitude: 14.8598,
115
          status: 'Available' as BicycleStatus,
116
          city: karlshamn,
117
        },
118
        // Service bikes
119
        {
120
          batteryLevel: 12,
121
          latitude: 56.1712,
122
          longitude: 14.8637,
123
          status: 'Service' as BicycleStatus,
124
          city: karlshamn,
125
        },
126
        {
127
          batteryLevel: 8,
128
          latitude: 56.1659,
129
          longitude: 14.8612,
130
          status: 'Service' as BicycleStatus,
131
          city: karlshamn,
132
        },
133
        {
134
          batteryLevel: 20,
135
          latitude: 56.1691,
136
          longitude: 14.8608,
137
          status: 'Service' as BicycleStatus,
138
          city: karlshamn,
139
        },
140
      ];
141
142
      // Jönköping bikes
143 9
      const jonkopingBikes = [
144
        // Available bikes
145
        {
146
          batteryLevel: 97,
147
          latitude: 57.7741,
148
          longitude: 14.2031,
149
          status: 'Available' as BicycleStatus,
150
          city: jonkoping,
151
        },
152
        {
153
          batteryLevel: 89,
154
          latitude: 57.7843,
155
          longitude: 14.1617,
156
          status: 'Available' as BicycleStatus,
157
          city: jonkoping,
158
        },
159
        {
160
          batteryLevel: 94,
161
          latitude: 57.7819,
162
          longitude: 14.1556,
163
          status: 'Available' as BicycleStatus,
164
          city: jonkoping,
165
        },
166
        // Service bikes
167
        {
168
          batteryLevel: 10,
169
          latitude: 57.7701,
170
          longitude: 14.2173,
171
          status: 'Service' as BicycleStatus,
172
          city: jonkoping,
173
        },
174
        {
175
          batteryLevel: 18,
176
          latitude: 57.7706,
177
          longitude: 14.2183,
178
          status: 'Service' as BicycleStatus,
179
          city: jonkoping,
180
        },
181
        {
182
          batteryLevel: 7,
183
          latitude: 57.7696,
184
          longitude: 14.2183,
185
          status: 'Service' as BicycleStatus,
186
          city: jonkoping,
187
        },
188
      ];
189
190 9
      await bicycleRepo.save([...goteborgBikes, ...karlshamnBikes, ...jonkopingBikes]);
191
    }
192
  }
193
}
194